using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Collections;

namespace RUPS
{
    public partial class AdaugaComponentaComandaForm : Form
    {
        public string idComanda;
        public string caleFisierCulori,
            caleFisierTipCanat,
            caleFisierPrivacyGlass;
        public string denCuloare,idCul;
        public static ArrayList 
            tipCanat, 
            tipSticla, 
            tipPrivacyGlass,culori;
        public static string tipSticlaAles,
                        plasaInsecte,
                        tipPlasaInsecte,
            nrCamere,
            nrBucati,
            IDTipMaterial,
            lungime,
            latime,
            tipPGAles,
            tipCanatAles,
            idCompComAleasa;
                

        public AdaugaComponentaComandaForm()
        {
            InitializeComponent();
            CenterToScreen();
            tipCanat = new ArrayList();
            tipSticla = new ArrayList();
            tipPrivacyGlass = new ArrayList();
            culori = new ArrayList();
            caleFisierCulori = AppDomain.CurrentDomain.BaseDirectory + "culori\\";
            caleFisierTipCanat = AppDomain.CurrentDomain.BaseDirectory + "cod comanda\\" + "tipCanat.txt";
            caleFisierPrivacyGlass = AppDomain.CurrentDomain.BaseDirectory + "privacy glass\\";
            int nrLiniiFile = 0;
            using (StreamReader stream = File.OpenText(caleFisierTipCanat))
            {
                
                string linie = stream.ReadLine();
                while (linie != null)
                {
                    tipCanat.Add(linie);
                    linie = stream.ReadLine();
                }
            }
           
            using (StreamReader stream = File.OpenText(caleFisierPrivacyGlass + "privacy glass.txt"))
            {
                string linie = stream.ReadLine();
                while (linie != null)
                {
                    tipPrivacyGlass.Add(linie);
                    linie=stream.ReadLine();
                }
            }

            using (StreamReader stream = File.OpenText(caleFisierCulori + "culori.txt"))
            {
                string linie = stream.ReadLine();
                while (linie != null)
                {
                    culori.Add(linie);
                    linie = stream.ReadLine();
                }
            }



            
        }

        private void AdaugaComponentaComandaForm_Load(object sender, EventArgs e)
        {
            plasaInsecteCheckBox.Checked = false;
            tipPlasaInsecteComboBox.Enabled = false;
            this.PGCheckBox.Checked = false;
            this.tipPrGlasscomboBox.Enabled = false;
            // TODO: This line of code loads data into the 'masivDBDataSet12.tipMaterial' table. You can move, or remove it, as needed.
            //this.tipMaterialTableAdapter1.Fill(this.masivDBDataSet12.tipMaterial);
            
           

            // TODO: This line of code loads data into the 'masivDBDataSet10.tipMaterial' table. You can move, or remove it, as needed.
            //this.tipMaterialTableAdapter.Fill(this.masivDBDataSet10.tipMaterial);
            this.comboBox1.DataSource = null;
            this.dataGridView1.DataSource = null;
            this.textBox1.Text = "";
            SqlDataAdapter da = new SqlDataAdapter("select * from comenziClienti where stareComanda='false'", comenziClientiTableAdapter.Connection);
            DataSet ds = new DataSet();
            da.Fill(ds, "comenziClienti");
            foreach (DataRow r in ds.Tables["comenziClienti"].Rows)
            {
                this.comboBox1.Items.Add(r["idComanda"].ToString());
            }
             //popularea grid-ului cu tipuri de materiale
            da = new SqlDataAdapter("select * from tipMaterial", tipMaterialTableAdapter1.Connection);
            da.Fill(ds, "tipMateriale");
            this.dataGridView2.DataSource = null;
            this.dataGridView2.DataSource = ds.Tables["tipMateriale"];
            int nrRand=0;
            foreach (DataRow r in ds.Tables["tipMateriale"].Rows)
            {
                idCul = r["IDCuloare"].ToString();
                da = new SqlDataAdapter("select numeCuloare from Culori where id=" + idCul, tipMaterialTableAdapter1.Connection);
                da.Fill(ds, "culori");

                string numeCul = ds.Tables["culori"].Rows[nrRand]["numeCuloare"].ToString();
                int idx = 0;
                foreach (string x in culori)
                {
                    if (x.Contains(numeCul) == true)
                        idx = culori.IndexOf(x);
                }
                idx += 1;
                string caleImagine = this.caleFisierCulori + "v"+idx+".jpg";
                Image im = new Bitmap(caleImagine);
                this.dataGridView2.Rows[nrRand].Cells[4].Value = new Bitmap(im);
                nrRand += 1;

            }
            
          
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            #region corect
            this.textBox1.Text = "";
            idComanda = this.comboBox1.SelectedItem.ToString();
            SqlDataAdapter da = new SqlDataAdapter("select * from comenziClienti where idComanda=" + idComanda, comenziClientiTableAdapter.Connection);
            DataSet ds = new DataSet();
            da.Fill(ds, "comenziClienti");

            SqlDataAdapter daCl = new SqlDataAdapter("select * from clienti where id=" + ds.Tables[0].Rows[0]["idClient"].ToString(), comenziClientiTableAdapter.Connection);
            DataSet dsCl = new DataSet();
            daCl.Fill(dsCl, "clienti");
            textBox1.Text += "Nume si prenume client: " + dsCl.Tables["clienti"].Rows[0]["nume"].ToString() + " " +
                dsCl.Tables["clienti"].Rows[0]["prenume"].ToString() + "\r\n";
            textBox1.Text += "Data comanda: " + ds.Tables[0].Rows[0]["dataComanda"].ToString() + "\r\n";
            textBox1.Text += "Data livrare: " + ds.Tables[0].Rows[0]["dataLivrare"].ToString();

            ////selectare componente comanda din BD pt afisarea lor in gridul de componente
            this.dataGridView1.DataSource = null;
            this.dataGridView1.Rows.Clear();
            SqlDataAdapter daComp=new SqlDataAdapter("select * from componenteComenziClienti where idComanda="+idComanda,
                                    this.componenteComenziClientiTableAdapter1.Connection);
            DataSet dsComp = new DataSet();
            daComp.Fill(dsComp, "comp");
           
            ////golire grid cu componente comanda
            if (this.dataGridView1.Rows.Count != 0)
            {
                this.dataGridView1.DataSource = null;
                this.dataGridView1.Rows.Clear();
            }

            if (dsComp.Tables["comp"] != null)
            {
                int nrRand = 0;
                foreach (DataRow r in dsComp.Tables["comp"].Rows)
                {
                    this.dataGridView1.Rows.Add();
                    dataGridView1.Rows[nrRand].Cells[0].Value = r["idComponentaComanda"].ToString();
                    dataGridView1.Rows[nrRand].Cells[1].Value = r["idTipMaterial"].ToString();
                    dataGridView1.Rows[nrRand].Cells[2].Value = r["lungime"].ToString();
                    dataGridView1.Rows[nrRand].Cells[3].Value = r["latime"].ToString();
                    
                    //detectarea culorii materialului
                    SqlDataAdapter daTipMat = new SqlDataAdapter("select idCuloare from tipMaterial where id=" + r["idTipMaterial"].ToString(), comenziClientiTableAdapter.Connection);
                    DataSet dsTipMat = new DataSet();
                    daTipMat.Fill(dsTipMat, "tipMat");
                    if (dsTipMat.Tables["tipMat"] != null)
                    {
                        idCul = dsTipMat.Tables["tipMat"].Rows[0]["IDculoare"].ToString();

                        //caut denumirea culorii
                        SqlDataAdapter daCul = new SqlDataAdapter("select numeCuloare from culori where id=" + idCul, comenziClientiTableAdapter.Connection);
                        daCul.Fill(dsTipMat, "denCul");
                        if (dsTipMat.Tables["denCul"] != null)//exista culoarea
                        {
                            denCuloare = dsTipMat.Tables["denCul"].Rows[0]["numeCuloare"].ToString();
                        }
                    }
                    string pathFileCuloare = caleFisierCulori + "v" + idCul + ".jpg";
                    
                    //selectarea culorii pp construirea unei imagini bitmap
                    Image bitmap = new Bitmap(pathFileCuloare);
                    dataGridView1.Rows[nrRand].Cells[4].Value = new Bitmap(bitmap);
                    dataGridView1.Rows[nrRand].Cells[5].Value = r["tipCanat"].ToString();
                    dataGridView1.Rows[nrRand].Cells[6].Value = r["tipPravacyGlass"].ToString();
                    if (r["tipPravacyGlass"].ToString() != "")
                    {
                        caleFisierPrivacyGlass = AppDomain.CurrentDomain.BaseDirectory + "privacy glass\\";
                        string pathFilePrivacyGlass = caleFisierPrivacyGlass;
                        pathFilePrivacyGlass += r["tipPravacyGlass"].ToString();
                        pathFilePrivacyGlass += ".bmp";
                        bitmap = new Bitmap(pathFilePrivacyGlass);
                        dataGridView1.Rows[nrRand].Cells[7].Value = new Bitmap(bitmap);
                    }
                    else
                    {
                        string pathFilePrivacyGlass = caleFisierPrivacyGlass + "nimic.bmp";
                        bitmap = new Bitmap(pathFilePrivacyGlass);
                        dataGridView1.Rows[nrRand].Cells[7].Value = new Bitmap(bitmap);
                    }
                    dataGridView1.Rows[nrRand].Cells[8].Value = r["plasaInsecte"].ToString();
                    dataGridView1.Rows[nrRand].Cells[9].Value = r["tipPlasaInsecte"].ToString();
                    dataGridView1.Rows[nrRand].Cells[10].Value = r["tipSticla"].ToString();
                    dataGridView1.Rows[nrRand].Cells[11].Value = r["nrCamere"].ToString();
                    dataGridView1.Rows[nrRand].Cells[12].Value = r["nrBucati"].ToString();
                    dataGridView1.Rows[nrRand].Cells[13].Value = r["IDComanda"].ToString();
                    nrRand += 1;
                }
            }
            #endregion
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void tipCanatcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.tipCanatPictureBox.BackgroundImage = null;
            string pathFileTipCanat=caleFisierTipCanat;
            string text=this.tipCanatcomboBox.SelectedIndex.ToString();
            caleFisierTipCanat = AppDomain.CurrentDomain.BaseDirectory + "cod comanda\\";
            caleFisierTipCanat += this.tipCanatcomboBox.SelectedIndex.ToString()+".bmp";
            Image imagine = new Bitmap(this.caleFisierTipCanat);
            this.tipCanatPictureBox.BackgroundImage = new Bitmap(imagine);
            if (tipCanatcomboBox.SelectedItem != null)
                tipCanatAles = tipCanatcomboBox.SelectedItem.ToString();
        }

        private void tipPrGlasscomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string denTipPG = this.tipPrGlasscomboBox.SelectedItem.ToString();
            caleFisierPrivacyGlass = AppDomain.CurrentDomain.BaseDirectory + "privacy glass\\"+denTipPG+".bmp";
            Image imagine = new Bitmap(this.caleFisierPrivacyGlass);
            this.privacyGlassPictureBox.BackgroundImage = new Bitmap(imagine);
            tipPGAles = denTipPG;
        }

        private void tipSticlaComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tipSticlaComboBox.SelectedItem != null)
                tipSticlaAles = tipSticlaComboBox.SelectedItem.ToString();
        }

        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                ClearForm();
                this.idComanda = dataGridView1.SelectedRows[0].Cells[13].Value.ToString();
                idCompComAleasa = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                tipCanatAles = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                tipPGAles = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
                plasaInsecte = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
                tipPlasaInsecte = dataGridView1.SelectedRows[0].Cells[9].Value.ToString();
                tipSticlaAles = dataGridView1.SelectedRows[0].Cells[10].Value.ToString();
                nrCamere = dataGridView1.SelectedRows[0].Cells[11].Value.ToString();
                nrBucati = dataGridView1.SelectedRows[0].Cells[12].Value.ToString();
                idComanda = dataGridView1.SelectedRows[0].Cells[13].Value.ToString();
                IDTipMaterial = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                lungime = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                latime = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();

                //populare form cu datele despre comanda
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void latimeTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' ||
                e.KeyChar == 8 || e.KeyChar == ',')
                base.OnKeyPress(e);
            else
                e.Handled = true;
        }

        private void lungimeTextBox_TextChanged(object sender, EventArgs e)
        {
            if(lungimeTextBox.Text!="")
                lungime = lungimeTextBox.Text;

        }

        private void nrCameretextBox_TextChanged(object sender, EventArgs e)
        {
            
        }
        private void nrBucatitextBox_TextChanged(object sender, EventArgs e)
        {
            if (nrBucatitextBox.Text != "")
                nrBucati = nrBucatitextBox.Text;
        }

        private void nrBucatitextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' ||
                e.KeyChar == 8 || e.KeyChar == ',')
                base.OnKeyPress(e);
            else
                e.Handled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string eroare = "";
            if (lungimeTextBox.Text == "")
                eroare += "Inserati lungimea componentei!\r\n";
            if (latimeTextBox.Text == "")
                eroare += "Inserati latimea componentei!\r\n";
            if (tipSticlaComboBox.SelectedItem == null)
                eroare += "Selectati tipul sticlei!\r\n";
            if (plasaInsecteCheckBox.Checked == true &&
                tipPlasaInsecteComboBox.SelectedItem == null)
                eroare += "Selectati tipul plasei de insecte! \r\n";
            if (nrCamereComboBox.SelectedItem == null)
                eroare += "Selectati nr. de camere! \r\n";
            if (tipCanatcomboBox.SelectedItem == null)
                eroare += "Selectati tipul canatului! \r\n";
            if(PGCheckBox.Checked==true && 
                tipPrGlasscomboBox.SelectedItem == null)
                eroare += "Selectati tipul de privacy glass! ";
            if (IDTipMaterial == "")
                eroare += "Selectati tipul de material!";

            
            if (eroare != "")
                MessageBox.Show(eroare);
            else
            {
                if (plasaInsecte == "1" &&
                    PGCheckBox.Checked == true)
                {
                    //inserez in baza de date noua componenta
                    //dacam se selecteaza si plasa de insecte si privacy glass
                    SqlCommand comm = this.comenziClientiTableAdapter.Connection.CreateCommand();
                    comm.CommandText = "insert into componenteComenziClienti(tipCanat,tipPlasaInsecte,plasaInsecte,tipSticla,nrCamere,nrBucati,IDComanda,IDTipMaterial,lungime,latime,tipPrivacyGlass) values('" +
                        tipCanatAles + "','" +
                        tipPlasaInsecte + "','" +
                        plasaInsecte + "','" +
                        tipSticlaAles + "','" +
                        nrCamere + "'," +
                        nrBucati + "," +
                        idComanda + "," +
                        IDTipMaterial + "," +
                        lungime + "," +
                        latime + ",'"+
                        tipPGAles+"')";
                    if (comenziClientiTableAdapter.Connection.State == ConnectionState.Closed)
                        comenziClientiTableAdapter.Connection.Open();
                    comm.ExecuteNonQuery();
                }
                else
                {   
                    //am plasa insecte dar nu am privacy Glass
                    if (plasaInsecte == "1" &&
                        PGCheckBox.Checked == false)
                    {
                        SqlCommand comm = this.comenziClientiTableAdapter.Connection.CreateCommand();
                        comm.CommandText = "insert into componenteComenziClienti(tipCanat,tipPlasaInsecte,plasaInsecte,tipSticla,nrCamere,nrBucati,IDComanda,IDTipMaterial,lungime,latime) values('" +
                            tipCanatAles + "','" +
                            tipPlasaInsecte + "','" +
                            plasaInsecte + "','" +
                            tipSticlaAles + "','" +
                            nrCamere + "'," +
                            nrBucati + "," +
                            idComanda + "," +
                            IDTipMaterial + "," +
                            lungime + "," +
                            latime + ")";
                        if (comenziClientiTableAdapter.Connection.State == ConnectionState.Closed)
                            comenziClientiTableAdapter.Connection.Open();
                        comm.ExecuteNonQuery();
                    }
                    else
                    {
                        //nu am plasa de insecte dar am privacy glass
                        if (plasaInsecte == "0" && tipPGAles != "")
                        {
                            SqlCommand comm = this.comenziClientiTableAdapter.Connection.CreateCommand();
                            comm.CommandText = "insert into componenteComenziClienti(tipCanat,plasaInsecte,tipSticla,nrCamere,nrBucati,IDComanda,IDTipMaterial,lungime,latime,privacyGlass) values('" +
                                tipCanatAles + "','" +
                                plasaInsecte + "','" +
                                tipSticlaAles + "','" +
                                nrCamere + "'," +
                                nrBucati + "," +
                                idComanda + "," +
                                IDTipMaterial + "," +
                                lungime + "," +
                                latime + ",'" +
                                tipPGAles + "')";
                            if (comenziClientiTableAdapter.Connection.State == ConnectionState.Closed)
                                comenziClientiTableAdapter.Connection.Open();
                            comm.ExecuteNonQuery();
                        }
                        else
                        {
                            if (plasaInsecte == "0" &&
                                tipPGAles == "")
                            {
                                SqlCommand comm = this.comenziClientiTableAdapter.Connection.CreateCommand();
                                comm.CommandText = "insert into componenteComenziClienti(tipCanat,tipSticla,nrCamere,nrBucati,IDComanda,IDTipMaterial,lungime,latime) values('" +
                                    tipCanatAles + "','" +
                                    tipSticlaAles + "','" +
                                    nrCamere + "'," +
                                    nrBucati + "," +
                                    idComanda + "," +
                                    IDTipMaterial + "," +
                                    lungime + "," +
                                    latime + ")"; 
                                if (comenziClientiTableAdapter.Connection.State == ConnectionState.Closed)
                                    comenziClientiTableAdapter.Connection.Open();
                                comm.ExecuteNonQuery();
                            }
                        }
                    }
                }
                
                //se face refresh la gridul in care apar Componentele comenzii
                //RefreshComponenteComanda();
                comboBox1_SelectedIndexChanged(sender, e);
                this.ClearForm();
            }
        }

        private void RefreshComponenteComanda()
        {
            dataGridView1.DataSource = null;
            dataGridView1.Rows.Clear();
            SqlDataAdapter da = new SqlDataAdapter("select * from componenteComenziClienti", componenteComenziClientiTableAdapter1.Connection
                );
            DataSet ds = new DataSet();
            da.Fill(ds, "comp");
            dataGridView1.DataSource = ds.Tables["comp"];
        }
        private void latimeTextBox_TextChanged(object sender, EventArgs e)
        {
            if (latimeTextBox.Text != "")
                latime = latimeTextBox.Text;
        }

        private void plasaInsecteCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (plasaInsecteCheckBox.Checked == true)
            {
                plasaInsecte = "true";
                tipPlasaInsecteComboBox.Enabled = true;
            }
            else
            {
                plasaInsecte = "false";
                tipPlasaInsecteComboBox.Enabled = false;
            }
        }

        private void tipPlasaInsecteComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (plasaInsecteCheckBox.Checked == true)
            {
                if (tipPlasaInsecteComboBox.SelectedItem != null)
                {
                    tipPlasaInsecte = tipPlasaInsecteComboBox.SelectedItem.ToString();
                }
            }
                
        }

        private void nrCamereComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (nrCamereComboBox.SelectedItem != null)
            {
                nrCamere = nrCamereComboBox.SelectedItem.ToString();
            }
        }

        private void PGCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (PGCheckBox.Checked == false)
            {
                this.privacyGlassPictureBox.BackgroundImage = null;
                this.tipPrGlasscomboBox.Enabled = false;
                plasaInsecte = "false";
            }
            else
            {
                this.tipPrGlasscomboBox.Enabled = true;
                plasaInsecte = "true";
            }
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
                
        }

        private void dataGridView2_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            IDTipMaterial = dataGridView2.SelectedRows[0].Cells[0].Value.ToString();
        }
        private void ClearForm()
        {
            this.lungimeTextBox.Text = "";
            this.latimeTextBox.Text = "";
            this.tipSticlaComboBox.SelectedText = null;
            this.tipSticlaComboBox.SelectedItem = null;
            this.plasaInsecteCheckBox.Checked = false;
            this.tipPlasaInsecteComboBox.SelectedText = "";
            this.tipPlasaInsecteComboBox.SelectedItem = null;
            this.nrCamereComboBox.SelectedText = "";
            this.nrCamereComboBox.SelectedItem = null;
            this.nrBucatitextBox.Text = "";
            this.tipCanatcomboBox.SelectedText = "";
            this.tipCanatcomboBox.SelectedItem = null;
            
            this.PGCheckBox.Checked = false;
            this.tipPrGlasscomboBox.SelectedItem = null;
            this.tipPrGlasscomboBox.SelectedText = null;
            this.tipPrGlasscomboBox.Enabled = false;
            this.tipPlasaInsecteComboBox.Enabled = false;
            this.tipCanatPictureBox.BackgroundImage = null;
            this.privacyGlassPictureBox.BackgroundImage = null;
        }

        private void lungimeTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9' ||
                e.KeyChar == 8 || e.KeyChar == ',')
                base.OnKeyPress(e);
            else
                e.Handled = true;
        }

 
    }
}